home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / C / LIB / UNIXLIB37B / !UnixLib37 / netlib / unix / c / dev
Text File  |  1996-11-06  |  8KB  |  472 lines

  1. /****************************************************************************
  2.  *
  3.  * $Source: /unixb/home/unixlib/source/unixlib37/src/unix/c/RCS/dev,v $
  4.  * $Date: 1996/10/30 21:59:01 $
  5.  * $Revision: 1.2 $
  6.  * $State: Exp $
  7.  * $Author: unixlib $
  8.  *
  9.  * $Log: dev,v $
  10.  * Revision 1.2  1996/10/30 21:59:01  unixlib
  11.  * Massive changes made by Nick Burret and Peter Burwood.
  12.  *
  13.  * Revision 1.1  1996/04/19 21:35:27  simon
  14.  * Initial revision
  15.  *
  16.  ***************************************************************************/
  17.  
  18. static const char rcs_id[] = "$Id: dev,v 1.2 1996/10/30 21:59:01 unixlib Exp $";
  19.  
  20. #include <errno.h>
  21. #include <stdlib.h>
  22. #include <fcntl.h>
  23. #include <unistd.h>
  24. #include <sys/unix.h>
  25. #include <sys/param.h>
  26. #include <sys/dev.h>
  27. #include <sys/os.h>
  28. #include <sys/netdb.h>
  29.  
  30. #define IGNORE(x) x = x
  31.  
  32. struct dev __dev[NDEV] =
  33. {
  34.   {__fsopen, __fsclose, __fsread, __fswrite, __fslseek, __fsioctl},
  35.   {__ttyopen, __ttyclose, __ttyread, __ttywrite, __ttylseek, __ttyioctl},
  36.   {__pipeopen, __pipeclose, __piperead, __pipewrite, __pipelseek, __pipeioctl},
  37.   {__nullopen, __nullclose, __nullread, __nullwrite, __nulllseek, __nullioctl},
  38. #ifdef DEV_SOCKET
  39.   {__sockopen, __sockclose, __sockread, __sockwrite, __socklseek, __sockioctl}
  40. #endif
  41. };
  42.  
  43. int
  44. __fsopen (char *file, int mode, struct file *f)
  45.  
  46. {
  47.   register int oflag = f->oflag;
  48.   register int *r = f->r;
  49.   _kernel_oserror *e;
  50.   int fd;
  51.  
  52.   file = __uname (file, oflag & (O_CREAT | O_OMASK));
  53.  
  54.   if (e = os_file (0x05, file, r))
  55.     {
  56.       __seterr (e);
  57.       return (-1);
  58.     }
  59.  
  60.   if (r[0])
  61.     {
  62.       if ((oflag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
  63.     {
  64.       errno = EEXIST;
  65.       return (-1);
  66.     }
  67.       if (oflag & O_CREAT)
  68.     oflag &= ~O_CREAT;
  69.       if (oflag & O_OMASK)
  70.     if (e = os_file (0x09, file, r))
  71.       {
  72.         __seterr (e);
  73.         return (-1);
  74.       }
  75.     }
  76.   else
  77.     {
  78.       if ((oflag & (O_RDONLY | O_CREAT)) == O_RDONLY)
  79.     {
  80.       errno = ENOENT;
  81.       return (-1);
  82.     }
  83.       if (oflag & O_OMASK)
  84.     oflag |= O_CREAT;
  85.     }
  86.  
  87.   if (oflag & O_CREAT)
  88.     {
  89.       r[2] = (oflag & O_BINARY) ? 0xffd : 0xfff;
  90.       r[4] = r[5] = 0;
  91.       if (e = os_file (0x0b, file, r))
  92.     {
  93.       __seterr (e);
  94.       return (-1);
  95.     }
  96.  
  97.       mode &= ~(__u->umask & 0777);
  98.  
  99.       r[5] = ((mode & 0400) >> 8) | ((mode & 0200) >> 6) |
  100.     ((mode & 0004) << 2) | ((mode & 0002) << 4);
  101.  
  102.       if (e = os_file (0x04, file, r))
  103.     {
  104.       __seterr (e);
  105.       return (-1);
  106.     }
  107.     }
  108.  
  109.   if (e = os_file (0x05, file, r))
  110.     {
  111.       __seterr (e);
  112.       return (-1);
  113.     }
  114.  
  115.   if (oflag & O_TRUNC)
  116.     e = os_fopen (0x80, file, &fd);
  117.   else
  118.     switch (oflag & O_OMASK)
  119.       {
  120.       case O_RDONLY:
  121.     e = os_fopen (0x40, file, &fd);
  122.     break;
  123.       case O_WRONLY:
  124.       case O_RDWR:
  125.     e = os_fopen (0xc0, file, &fd);
  126.     break;
  127.       default:
  128.     errno = EINVAL;
  129.     return (-1);
  130.     break;
  131.       }
  132.  
  133.   if (e)
  134.     {
  135.       __seterr (e);
  136.       return (-1);
  137.     }
  138.  
  139.   return (fd);
  140. }
  141.  
  142. int
  143. __fsclose (int fd, struct file *f)
  144.  
  145. {
  146.   _kernel_oserror *e;
  147.  
  148.   IGNORE (f);
  149.  
  150.   if (e = os_fclose (fd))
  151.     {
  152.       __seterr (e);
  153.       return (-1);
  154.     }
  155.  
  156.   return (0);
  157. }
  158.  
  159. int
  160. __fsread (int fd, register void *data, register int nbyte, struct file *f)
  161.  
  162. {
  163.   int r[5];
  164.   _kernel_oserror *e;
  165.  
  166.   IGNORE (f);
  167.  
  168.   if (e = os_fread (fd, data, nbyte, r))
  169.     {
  170.       __seterr (e);
  171.       return (-1);
  172.     }
  173.  
  174.   return (nbyte - r[3]);
  175. }
  176.  
  177. int
  178. __fswrite (int fd, register void *data, register int nbyte, struct file *f)
  179.  
  180. {
  181.   int r[5];
  182.   _kernel_oserror *e;
  183.  
  184.   IGNORE (f);
  185.  
  186.   if (e = os_fwrite (fd, data, nbyte, r))
  187.     {
  188.       __seterr (e);
  189.       return (-1);
  190.     }
  191.  
  192.   return (nbyte - r[3]);
  193. }
  194.  
  195. __off_t
  196. __fslseek (int fd, __off_t lpos, int whence, struct file *f)
  197.  
  198. {
  199.   int r[3];
  200.   _kernel_oserror *e;
  201.  
  202.   IGNORE (f);
  203.  
  204.   switch (whence)
  205.     {
  206.     case 0:
  207.       if (e = os_args (1, fd, (int) lpos, r))
  208.     {
  209.       __seterr (e);
  210.       return (-1);
  211.     }
  212.       break;
  213.     case 1:
  214.       if (e = os_args (0, fd, 0, r))
  215.     {
  216.       __seterr (e);
  217.       return (-1);
  218.     }
  219.       if (e = os_args (1, fd, r[2] + (int) lpos, r))
  220.     {
  221.       __seterr (e);
  222.       return (-1);
  223.     }
  224.       break;
  225.     case 2:
  226.       if (e = os_args (2, fd, 0, r))
  227.     {
  228.       __seterr (e);
  229.       return (-1);
  230.     }
  231.       if (e = os_args (1, fd, r[2] + (int) lpos, r))
  232.     {
  233.       __seterr (e);
  234.       return (-1);
  235.     }
  236.       break;
  237.     default:
  238.       errno = EINVAL;
  239.       return (-1);
  240.       break;
  241.     }
  242.  
  243.   return ((long) r[2]);
  244. }
  245.  
  246. int
  247. __fsioctl (int fd, register int request, void *arg, struct file *f)
  248.  
  249. {
  250.   IGNORE (fd);
  251.   IGNORE (request);
  252.   IGNORE (arg);
  253.   IGNORE (f);
  254.   errno = EINVAL;
  255.   return (-1);
  256. }
  257.  
  258. struct pipe *__pipe = NULL;
  259.  
  260. int
  261. __pipeopen (char *file, int mode, struct file *f)
  262.  
  263. {
  264.   return (__fsopen (file, mode, f));
  265. }
  266.  
  267. int
  268. __pipeclose (int fd, struct file *f)
  269.  
  270. {
  271.   struct pipe *pi = __pipe, *pi_ = 0;
  272.  
  273.   while (pi)
  274.     {
  275.       if (pi->p[0] == f || pi->p[1] == f)
  276.     break;
  277.       pi_ = pi;
  278.       pi = pi->next;
  279.     }
  280.   if (!pi)
  281.     {
  282.       errno = EBADF;
  283.       return (-1);
  284.     }
  285.   if (pi_)
  286.     pi_->next = pi->next;
  287.   else
  288.     __pipe = pi->next;
  289.   __fsclose (fd, f);
  290.   {
  291.     _kernel_oserror *e;
  292.  
  293.     if (e = os_fsctrl (27, __uname (pi->file, 0), 0, 0642))
  294.       {
  295.     __seterr (e);
  296.     return (-1);
  297.       }
  298.   }
  299.   free (pi->file);
  300.   free (pi);
  301.   return (0);
  302. }
  303.  
  304. int
  305. __piperead (int fd, void *data, int nbyte, struct file *f)
  306.  
  307. {
  308.   if (f->oflag & O_PIPE)
  309.     {
  310.       f->oflag &= ~O_PIPE;
  311.       __fslseek (fd, 0, 0, f);
  312.     }
  313.   return (__fsread (fd, data, nbyte, f));
  314. }
  315.  
  316. int
  317. __pipewrite (int fd, void *data, int nbyte, struct file *f)
  318.  
  319. {
  320.   if (f->oflag & O_PIPE)
  321.     return (__fswrite (fd, data, nbyte, f));
  322.   else
  323.     {
  324.       errno = EPIPE;
  325.       return (-1);
  326.     }
  327. }
  328.  
  329. __off_t
  330. __pipelseek (int fd, __off_t lpos, int whence, struct file *f)
  331.  
  332. {
  333.   IGNORE (fd);
  334.   IGNORE (lpos);
  335.   IGNORE (whence);
  336.   IGNORE (f);
  337.   errno = ESPIPE;
  338.   return (-1);
  339. }
  340.  
  341. int
  342. __pipeioctl (int fd, register int request, void *arg, struct file *f)
  343.  
  344. {
  345.   IGNORE (fd);
  346.   IGNORE (request);
  347.   IGNORE (arg);
  348.   IGNORE (f);
  349.   errno = EINVAL;
  350.   return (-1);
  351. }
  352.  
  353. int
  354. __nullopen (char *file, int mode, struct file *f)
  355.  
  356. {
  357.   IGNORE (file);
  358.   IGNORE (mode);
  359.   IGNORE (f);
  360.   return (0);
  361. }
  362.  
  363. int
  364. __nullclose (int fd, struct file *f)
  365.  
  366. {
  367.   IGNORE (fd);
  368.   IGNORE (f);
  369.   return (0);
  370. }
  371.  
  372. int
  373. __nullread (int fd, void *data, int nbyte, struct file *f)
  374.  
  375. {
  376.   IGNORE (fd);
  377.   IGNORE (data);
  378.   IGNORE (nbyte);
  379.   IGNORE (f);
  380.   return (0);
  381. }
  382.  
  383. int
  384. __nullwrite (int fd, void *data, int nbyte, struct file *f)
  385.  
  386. {
  387.   IGNORE (fd);
  388.   IGNORE (data);
  389.   IGNORE (f);
  390.   return (nbyte);
  391. }
  392.  
  393. __off_t
  394. __nulllseek (int fd, __off_t lpos, int whence, struct file *f)
  395.  
  396. {
  397.   IGNORE (fd);
  398.   IGNORE (lpos);
  399.   IGNORE (whence);
  400.   IGNORE (f);
  401.   errno = ESPIPE;
  402.   return (-1);
  403. }
  404.  
  405. int
  406. __nullioctl (int fd, int request, void *arg, struct file *f)
  407.  
  408. {
  409.   IGNORE (fd);
  410.   IGNORE (request);
  411.   IGNORE (arg);
  412.   IGNORE (f);
  413.   errno = EINVAL;
  414.   return (-1);
  415. }
  416.  
  417. #ifdef DEV_SOCKET
  418.  
  419. /* We optionally include the network support code - mainly because I cannot
  420.  * be bothered to recompile it all the time.
  421.  */
  422.  
  423. int
  424. __sockopen (char *file, int mode, struct file *f)
  425. {
  426.   IGNORE (file);
  427.   IGNORE (mode);
  428.   IGNORE (f);
  429.   errno = EOPNOTSUPP;
  430.   return (-1);
  431. }
  432.  
  433. int
  434. __sockclose (int fd, struct file *f)
  435. {
  436.   IGNORE (f);
  437.   return (_sclose (fd));
  438. }
  439.  
  440. int
  441. __sockread (int fd, void *data, int nbyte, struct file *f)
  442. {
  443.   IGNORE (f);
  444.   return (_sread (fd, data, nbyte));
  445. }
  446.  
  447. int
  448. __sockwrite (int fd, void *data, int nbyte, struct file *f)
  449. {
  450.   IGNORE (f);
  451.   return (_swrite (fd, data, nbyte));
  452. }
  453.  
  454. __off_t
  455. __socklseek (int fd, __off_t lpos, int whence, struct file *f)
  456. {
  457.   IGNORE (fd);
  458.   IGNORE (lpos);
  459.   IGNORE (whence);
  460.   IGNORE (f);
  461.   errno = EOPNOTSUPP;
  462.   return (-1);
  463. }
  464.  
  465. int
  466. __sockioctl (int fd, int request, void *arg, struct file *f)
  467. {
  468.   IGNORE (f);
  469.   return (_sioctl (fd, request, arg));
  470. }
  471. #endif
  472.